package com.hc.lookSys.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.hc.lookSys.pojo.UserInfo;
import com.hc.lookSys.util.MyCons;


public class UserInfoDao {


	public UserInfo findUserlogin(String loginAccount, String loginPwd) {
		UserInfo userInfo = null;
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "SELECT user_id,user_code,email,gender,register_time,last_logintime from users  where user_code =?  and password =?";
		try {
			Class.forName(MyCons.DRIVER);
			
			connection = DriverManager.getConnection(MyCons.URL, MyCons.USER, MyCons.PASSWORD);
			ps = connection.prepareStatement(sql);
			ps.setString(1, loginAccount);
			ps.setString(2, loginPwd);
			// ?赋值 ps.setXxx(值的下标[1开始],值);Xxx 指的是值的数据类型
			rs = ps.executeQuery();
			while (rs.next()) {
				userInfo = new UserInfo();
				userInfo.setUserId(rs.getInt("user_id"));
				userInfo.setUserCode(rs.getString("user_code"));
				userInfo.setEmail(rs.getString("email"));
				userInfo.setGender(rs.getNString("gender"));
				userInfo.setRegisterTime(rs.getDate("register_time"));
				userInfo.setLastLoginTime(rs.getDate("last_logintime"));
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != rs) {
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			if (null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (null != connection) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		return userInfo;
	}

}
